package com.ruoyi.common.flowable;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.dto.FlowTaskDTO;
import com.ruoyi.mapper.ActHiProcinstMapper;
import com.ruoyi.service.FlowInstanceExtServiceImpl;
import com.ruoyi.service.SysUserServiceImpl;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.flowable.engine.task.Comment;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 任务节点前置监听处理类
 * @author: Lu Yang
 * @create: 2019-05-04 20:51
 **/
@Component
public class ProcessCompletedListener implements FlowableEventListener {

    public FlowInstanceExtServiceImpl getFlowInstanceExtServiceImpl() {
        return SpringUtils.getBean(FlowInstanceExtServiceImpl.class);
    }

    public SysUserServiceImpl getSysUserService() {
        return SpringUtils.getBean(SysUserServiceImpl.class);
    }

    public ActHiProcinstMapper getActHiProcinstMapper() {
        return SpringUtils.getBean(ActHiProcinstMapper.class);
    }

    public TaskService getTaskService() {
        return SpringUtils.getBean(TaskService.class);
    }

    public RepositoryService getRepositoryService() {
        return SpringUtils.getBean(RepositoryService.class);
    }

    public HistoryService getHistoryService() {
        return SpringUtils.getBean(HistoryService.class);
    }

    /*private List<String> getSponsorDirector(Long deptId){
        List<String> users = getSysUserService().getBaseMapper().getSponsorDirector(deptId);
        return users;
    }

    private List<String> getSponsorColleague(Long deptId){
        List<String> users = getSysUserService().getBaseMapper().getSponsorDirector(deptId);
        return users;
    }*/



    @Override
    public void onEvent(FlowableEvent event) {
       if(event instanceof FlowableEntityEventImpl){
           String comment = "审批完成";
           ExecutionEntityImpl executionEntity =  (ExecutionEntityImpl) ((FlowableEntityEventImpl)event).getEntity();
           List<Comment> commentList = getTaskService().getProcessInstanceComments(executionEntity.getProcessInstanceId());
           long count = commentList.stream().filter(r->"6".equals(r.getType())).count();
           if(count == 0){
               getTaskService().addComment(null,executionEntity.getProcessInstanceId(), FlowComment.SUCCESS_FINISH.getCode(), comment);
           }
           getFlowInstanceExtServiceImpl().updateExt(executionEntity.getProcessInstanceId());

           FlowTaskDTO flowTaskDto = getActHiProcinstMapper().getByProcInsId(executionEntity.getProcessInstanceId());
           List<HistoricTaskInstance> taskInstanceList = getHistoryService().createHistoricTaskInstanceQuery().processInstanceId(executionEntity.getProcessInstanceId()).list();
           if(taskInstanceList.size() > 0){
               sendWxMsgSuccessAudit(taskInstanceList.get(0),flowTaskDto.getStartUserId());
           }
       }
    }


    private void sendWxMsgSuccessAudit(HistoricTaskInstance task, String userId){
        SysUser sysUser = getSysUserService().getById(userId);
        /*if(sysUser.getOpenId() == null){
            return;
        }*/
        FlowTaskDTO taskDto = getActHiProcinstMapper().getByProcInsId(task.getProcessInstanceId());
        if(taskDto == null){
            return;
        }
        /*String openId = sysUser.getOpenId();
        String templateId = Enum.WX_TEMPLATE.AUDIT_SUCCESS.getCode();
        String url = "https://kt.beitone.com/mobile/audit/detail?id="+task.getId()+"&menu=waitToDoList";
        Map<String, Data> data = new HashMap<>();
        data.put("first", new Data(taskDto.getBusinessName(), "#173177"));
        data.put("keyword1", new Data("通过审核", "#173177"));
        data.put("keyword2", new Data(DateUtil.now(), "#173177"));
        data.put("remark", new Data("请关注相关审核意见", "#173177"));*/

        //TaskCreatedListener.wxSend( openId, templateId, url,data);
    }


    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}